package com.demo.aop.pcut.name;

import com.demo.aop.before.simple.Guitarist;
import com.demo.aop.pcut.stat.SimpleAdvice;
import com.demo.aop.sing.Singer;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;

/**
 * 描述：
 *
 * @author lida
 * @time 2019/10/23 15:10
 */
public class NamePointcutDemo {

	public static void main(String[] args) {
		SimpleNamedBean target = new SimpleNamedBean();

		//名称切面
		NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
		pointcut.addMethodName("sing");
		pointcut.addMethodName("talk");

		Advisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleAdvice());

		//代理  使用下面这句话将无法创建比接口拓展功能的子类转化
		//1 构造函数
		//public ProxyFactory(Object target) {
		//		setTarget(target);
		//		setInterfaces(ClassUtils.getAllInterfaces(target));
		//}
		//会去设置Interfaces
		//ProxyFactory proxyFactory = new ProxyFactory(target);
		//2 不会去设置Interfaces
		ProxyFactory proxyFactory = new ProxyFactory();
		proxyFactory.setTarget(target);

		proxyFactory.addAdvisor(advisor);

		//1将使用jdk动态代理（基于接口），2将使用Cglib动态代理（基于子类）
		SimpleNamedBean sb = (SimpleNamedBean) proxyFactory.getProxy();

		sb.sing();
		sb.sing(new Guitarist());
		sb.talk();
		sb.rest();
	}
}
